﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace WalutyNBP
{
    class Xml
    {
        private byte tabela;
        private Stream dane_c;
        public string data;
        public XmlTextReader xtr;
        public Xml(byte nr_tab)
        {
            this.tabela = nr_tab;
        }
        public bool Test()
        {
            HttpWebResponse response = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nbp.pl/home.aspx");
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    response.Close();
                    return true;
                }
                else return false;
            }
            catch { response.Close(); return false; }
        }
        public bool DataArch()
        {
            HttpWebResponse response = null;
            StreamReader data = null;
            bool loop = true;
            while (loop)
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.nbp.pl//Kursy/xml/dir.txt");
                    response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        data = new StreamReader(response.GetResponseStream());
                        string line, sub_line;
                        while ((line = data.ReadLine()) != null)
                        {
                            sub_line = line.Substring(5, 6);

                            if (this.tabela == 1 && line[0] == 'a' && Preferences.Data() == sub_line)
                            {
                                Preferences.adres = line;
                                Preferences.adres2 = Preferences.adres;
                                loop = false;
                                response.Close();
                                data.Close();
                                break;
                            }
                            if (this.tabela == 2 && line[0] == 'b' && Preferences.Data() == sub_line)
                            {
                                Preferences.adres = line;
                                Preferences.adres2 = Preferences.adres;
                                loop = false;
                                response.Close();
                                data.Close();
                                break;
                            }
                            if (this.tabela == 3 && line[0] == 'c' && Preferences.Data() == sub_line)
                            {
                                Preferences.adres = line;
                                Preferences.adres2 = Preferences.adres;
                                loop = false;
                                response.Close();
                                data.Close();
                                break;
                            }
                            if (this.tabela == 4 && line[0] == 'h' && Preferences.Data() == sub_line)
                            {
                                Preferences.adres = line;
                                Preferences.adres2 = Preferences.adres;
                                loop = false;
                                response.Close();
                                data.Close();
                                break;
                            }
                        }
                        response.Close();
                        data.Close();
                       // if (Preferences.aktarch)
                        {
                            if (loop)
                            {
                                Preferences.full_date = Preferences.full_date.AddDays(-1);
                                Preferences.dzien = Preferences.full_date.Day.ToString();
                                Preferences.miesiac = Preferences.full_date.Month.ToString();
                                Preferences.rok = Preferences.full_date.Year.ToString();
                            }
                            else
                            {
                                response.Close();
                                data.Close();
                                return true;
                            }
                        }
                        //else loop = false;
                    }
                    else 
                    {
                        response.Close();
                        data.Close();
                        return false;
                    }
                }
                catch { response.Close(); data.Close(); return true; }
            }
            return true;
        }
        public bool Data()
        {
            HttpWebResponse response = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.nbp.pl//Kursy/xml/dir.txt");
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader data = new StreamReader(response.GetResponseStream());
                    string line;
                    while ((line = data.ReadLine()) != null)
                    {
                        if (this.tabela == 1 && line[0] == 'a')
                        {
                            Preferences.adres = line;
                            Preferences.adres2 = Preferences.adres;
                        }
                        if (this.tabela == 2 && line[0] == 'b')
                        {
                            Preferences.adres = line;
                            Preferences.adres2 = Preferences.adres;
                        }
                        if (this.tabela == 3 && line[0] == 'c')
                        {
                            Preferences.adres = line;
                            Preferences.adres2 = Preferences.adres;
                        }
                        if (this.tabela == 4 && line[0] == 'h')
                        {
                            Preferences.adres = line;
                            Preferences.adres2 = Preferences.adres;
                        }
                    }
                    response.Close();
                    data.Close();
                    return true;
                }
                else return false;
            }
            catch { response.Close(); return true; }
        }

        private Stream Pobierz()
        {
            HttpWebResponse response = null;
            string adres = "http://nbp.pl/kursy/xml/" + Preferences.adres + ".xml";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(adres);
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //MessageBox.Show(Preferences.adres);
                    return response.GetResponseStream();
                }
                else return null;
            }
            catch { return null; }
        }

        public int Count()
        {
            if ((this.dane_c = this.Pobierz()) != null)
            {
                this.xtr = new XmlTextReader(this.dane_c);
                int ile = 0;
                while (this.xtr.Read())
                {
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "pozycja")
                    {
                        ile++;
                    }
                }
                this.xtr.Close();
                this.dane_c.Close();
                return ile;
            }
            else return 0;

        }

        public void PraserA(ref TabA[] tab)
        {
            int wsk = 0;
            string nazwa = "", przeli = "", kod = "", sredni = "";
            Stream dane = null;
            if ((dane = this.Pobierz()) != null)
            {
                this.xtr = new XmlTextReader(dane);
                while (this.xtr.Read())
                {
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "data_publikacji")
                        this.data = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "nazwa_waluty")
                        nazwa = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "przelicznik")
                        przeli = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kod_waluty")
                        kod = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kurs_sredni")
                        sredni = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.EndElement && this.xtr.Name == "pozycja")
                    {
                        tab[wsk] = new TabA(nazwa, kod, przeli, sredni);
                        wsk++;
                    }
                }
            }
        }
        public void PraserB(ref TabB[] tab)
        {
            int wsk = 0;
            string nazwak="", nazwa = "", przeli = "", kod = "", sredni = "";
            Stream dane = null;
            if ((dane = this.Pobierz()) != null)
            {
                this.xtr = new XmlTextReader(dane);
                while (this.xtr.Read())
                {
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "data_publikacji")
                        this.data = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "nazwa_kraju")
                        nazwak = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "nazwa_waluty")
                        nazwa = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "przelicznik")
                        przeli = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kod_waluty")
                        kod = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kurs_sredni")
                        sredni = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.EndElement && this.xtr.Name == "pozycja")
                    {
                        tab[wsk] = new TabB(nazwak, nazwa, kod, przeli, sredni);
                        wsk++;
                    }
                }
            }
        }
        public void PraserC(ref TabC[] tab)
        {
            int wsk = 0;
            string nazwa = "", przeli = "", kod = "", kupna = "", sprzedazy="";
            Stream dane = null;
            if ((dane = this.Pobierz()) != null)
            {
                this.xtr = new XmlTextReader(dane);
                while (this.xtr.Read())
                {
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "data_publikacji")
                        this.data = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "nazwa_waluty")
                        nazwa = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "przelicznik")
                        przeli = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kod_waluty")
                        kod = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kurs_kupna")
                        kupna = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kurs_sprzedazy")
                        sprzedazy = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.EndElement && this.xtr.Name == "pozycja")
                    {
                        tab[wsk] = new TabC(nazwa, kod, przeli, kupna, sprzedazy);
                        wsk++;
                    }
                }
            }
        }
        public void PraserH(ref TabH[] tab)
        {
            int wsk = 0;
            string nazwak = "", sym = "", nazwa = "", przeli = "", kupna = "", sprzedazy = "", sredni = "";
            Stream dane = null;
            if ((dane = this.Pobierz()) != null)
            {
                this.xtr = new XmlTextReader(dane);
                while (this.xtr.Read())
                {
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "data_publikacji")
                        this.data = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "nazwa_kraju")
                        nazwak = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "symbol_waluty")
                        sym = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "nazwa_waluty")
                        nazwa = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "przelicznik")
                        przeli = this.xtr.ReadString() + " ";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kurs_kupna")
                        kupna = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kurs_sprzedazy")
                        sprzedazy = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.Element && this.xtr.Name == "kurs_sredni")
                        sredni = this.xtr.ReadString() + "\n";
                    if (this.xtr.MoveToContent() == XmlNodeType.EndElement && this.xtr.Name == "pozycja")
                    {
                        tab[wsk] = new TabH(nazwak, sym, nazwa, przeli, kupna, sprzedazy, sredni);
                        wsk++;
                    }
                }
            }
        }

 
    }
}
